{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 本函数已保存在d2lzh包中⽅便以后使⽤\n",
    "import random\n",
    "import torch\n",
    "def data_iter(batch_size, features, labels):\n",
    "     num_examples = len(features)\n",
    "     indices = list(range(num_examples))\n",
    "     random.shuffle(indices) # 样本的读取顺序是随机的\n",
    "     for i in range(0, num_examples, batch_size):\n",
    "         j = torch.LongTensor(indices[i: min(i + batch_size, num_examples)]) # 最后⼀次可能不⾜⼀个batch\n",
    "         yield features.index_select(0, j), labels.index_select(0, j)\n",
    "        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}